package audio;

import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

/**
 * 
 * @author Ygor
 *
 */
class WavPlayer {

	void play(final AudioInputStream audioInputStream) {
		new Thread() {

			@Override
			public void run() {
				SourceDataLine sourceDataLine = null;
				try {
					AudioFormat audioFormat = audioInputStream.getFormat();
					DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
					sourceDataLine = (SourceDataLine) AudioSystem.getLine(info);
					sourceDataLine.open(audioFormat);
				} catch (LineUnavailableException e) {
					e.printStackTrace();
					return;
				}

				sourceDataLine.start();
				byte[] data = new byte[524288];
				try {
					int bytesRead = 0;
					while (bytesRead != -1) {
						bytesRead = audioInputStream.read(data, 0, data.length);
						if (bytesRead >= 0) {
							sourceDataLine.write(data, 0, bytesRead);
						}
					}
				} catch (IOException e) {
					e.printStackTrace();
					return;
				} finally {
					sourceDataLine.drain();
					sourceDataLine.close();
				}
			}
		}.start();
	}
}
